using System;
using System.Collections.Generic;
using System.Xml;

namespace XmlObjects.XmlWriter
{
	public class ListXmlWriter<T> : XmlWriterBase
	{
		private IXmlWriter baseWriter = XmlWriterFactory.GetWriter(typeof (T));

		public ListXmlWriter(ElementToPropertyMap elementToPropertyMap) : base(elementToPropertyMap)
		{
		}

		public override void Append(object value, XmlNode node)
		{
			List<T> valueList = (List<T>)value;
			//  What's my select string?
			//   What am I going to name my child elements?
			//   If there are no subtypes, assume that the name has no subtype.
			//   If there are subtypes, a wildcard can only be specified if 
			//     #name is specified in the FindByString


			if (elementToPropertyMap.HasSubTypes)
			{
				foreach (T anObject in valueList)
				{
					ElementToPropertyMap map = new ElementToPropertyMap(anObject.GetType());
					map.SubTypeMap = elementToPropertyMap.SubTypeMap;
					
					IXmlWriter writer = XmlWriterFactory.GetWriter(anObject.GetType(), elementToPropertyMap);
					XmlNode newNode = elementToPropertyMap.AppendNodeTo(node, anObject.GetType());
					writer.WriteLocal(anObject, newNode);
				}
			}
			else
			{
				foreach (T obj in valueList)
				{
					XmlNode newNode = elementToPropertyMap.AppendNodeTo(node, typeof(T));
					baseWriter.WriteLocal(obj, newNode);
				}
			}
		}

		public override void WriteLocal(object obj, XmlNode node)
		{
			throw new NotImplementedException();
		}
	}
}